package com.xc.pattern.observe;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xingchen
 * @description 消息发布者
 * @create 2022-09-04 20:53
 */
public class Subject {

    private List<Observer> observerList = new ArrayList<>();

    /**
     * 新增观察者
     *
     * @param observer
     */
    public void addObserver(Observer observer) {
        this.observerList.add(observer);
    }

    /**
     * 删除观察者
     *
     * @param observer
     */
    public void deleteObserver(Observer observer) {
        this.observerList.remove(observer);
    }

    public void notifyAllObserver() {
        for (Observer observer : this.observerList) {
            observer.update();
        }
    }
}
